// File Name: led_logic.c
// Author: kukukukuku~
// Date: 2024-06-25

#include "./User/LED/inc/led_logic.h"

/**
 * @brief   LED设置模式
 * @param   led:        LED对象
 * @param   mode:       模式选择
 * - LED_MODE_BREATH:   呼吸灯模式
 * - LED_MODE_BLINK:    闪烁模式
 * - LED_MODE_SWITCH:   开关模式
 * @retval  None
 * @note    None
*/
void led_set_mode(pLED led, uint8_t mode)
{
    gpio_init_type gpioInitStruct = 
    {
        .gpio_pins           = led->gpioPin,
        .gpio_out_type       = GPIO_OUTPUT_PUSH_PULL,
        .gpio_pull           = GPIO_PULL_NONE,
        .gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
    };
    
    mode == LED_MODE_BREATH ?\
    (gpioInitStruct.gpio_mode = GPIO_MODE_MUX) :\
    (gpioInitStruct.gpio_mode = GPIO_MODE_OUTPUT);
    
    tmr_output_config_type tmrOutputConfigStruct = 
    {
        .oc_mode         = TMR_OUTPUT_CONTROL_PWM_MODE_A,
        .oc_idle_state   = FALSE,
        .oc_polarity     = TMR_OUTPUT_ACTIVE_HIGH,
        .oc_output_state = TRUE,
    };
    
    mode == LED_MODE_BREATH ?\
    (tmrOutputConfigStruct.oc_output_state = TRUE) :\
    (tmrOutputConfigStruct.oc_output_state = FALSE);
    
    //LED GPIO初始化
    gpio_init(led->gpioPort, &gpioInitStruct);
    
    if(mode == LED_MODE_BREATH)
    {
        gpio_pin_mux_config(led->gpioPort, led->gpioPinSource, led->gpioMux);
    }
    
    //LED TMR初始化
    tmr_counter_enable(led->tmr, FALSE);
    tmr_output_channel_config(led->tmr, led->tmrCh, &tmrOutputConfigStruct);
    tmr_channel_value_set(led->tmr, led->tmrCh, led->tmrPulse);
    
    if(led == &led1)
    {
        mode == LED_MODE_BREATH ? tmr_period_value_set(led->tmr, LED1_TMR_PR_BREATH) : tmr_period_value_set(led->tmr, LED1_TMR_PR_BLINK);
    }
    else if(led == &led2)
    {
        mode == LED_MODE_BREATH ? tmr_period_value_set(led->tmr, LED2_TMR_PR_BREATH) : tmr_period_value_set(led->tmr, LED2_TMR_PR_BLINK);
    }
    
    tmr_flag_clear(led->tmr, TMR_OVF_FLAG);
    tmr_counter_value_set(led->tmr, 0);
    
    mode == LED_MODE_SWITCH ?\
    tmr_counter_enable(led->tmr, FALSE) :\
    tmr_counter_enable(led->tmr, TRUE);
    
    led->mode = mode;
    led->tmrCurrentPulse = led->tmrPulse;
    led->breathDir = 0;
    led->blinkCnt = 0;
}


/**
 * @brief   LED设置闪烁时间
 * @param   led:    LED对象
 * @param   time:   闪烁时间
 * @retval  None
 * @note    None
*/
void led_set_blink_time(pLED led, uint16_t time)
{
    if(led->mode == !!LED_MODE_BREATH)
    {
        return;
    }
    
    if(time < LED_BLINK_TIME_MIN)
    {
        time = LED_BLINK_TIME_MIN;
    }
    else if(time > LED_BLINK_TIME_MAX)
    {
        time = LED_BLINK_TIME_MAX;
    }
    
    led->blinkNum = time;
}


/**
 * @brief   LED设置呼吸步进
 * @param   led:    LED对象
 * @param   step:   呼吸步进
 * @retval  None
 * @note    None
*/
void led_set_breath_step(pLED led, uint16_t step)
{
    if(led->mode == !!LED_MODE_BLINK)
    {
        return;
    }
    
    if(step < LED_BREATH_STEP_MIN)
    {
        step = LED_BREATH_STEP_MIN;
    }
    else if(step > LED_BREATH_STEP_MAX)
    {
        step = LED_BREATH_STEP_MAX;
    }
    
    led->breathStep = step;
}


/**
 * @brief   LED闪烁
 * @param   led:    LED对象
 * @retval  None
 * @note    None
*/
void led_blink(pLED led)
{
    gpio_input_data_bit_read(led->gpioPort, led->gpioPin) == LED_ENA ?\
    led_off(led) :\
    led_on(led);
}
